package tests;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 */
public class MyProjectPage extends Utils {
    public static String url = "http://49.235.61.184:8080/project";

    public MyProjectPage() {
        super(url);
    }

    /**
     * 检查页面元素
     */
    public void checkMyProjectPageRight() {
        //检查页面标题
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > div > span"));
        //检查搜索框
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > input"));
        //检查“查询”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > span > button > span > svg"));
        //检查“新建”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button"));

    }
    /**
     * 测试一：创建调查问卷
     */
    public void createSurveyQuestionnaires() throws IOException, InterruptedException {
        // 点击“新建”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button")).click();

        //显式等待：最长等10秒,直到“问卷调查”选项可见
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        //用 XPath 文本定位
        WebElement surveyItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class, 'ant-dropdown-menu')]//span[text()='问卷调查']")));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        // 滚动到元素 + JS强制点击
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", surveyItem);
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", surveyItem);
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“保存”按钮
        driver.findElement(By.cssSelector("#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span")).click();
        //点击"项目"
        driver.findElement(By.cssSelector("#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(6) > a > button > span.anticon.anticon-home > svg")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //查询是否新建成功
        //在搜索框中输入搜索的内容,点击“查询”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > input")).sendKeys("请输入标题");
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > span > button > span > svg")).click();
        //检查是否新建成功
        String str = driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-list.ant-list-split.ant-list-grid.survey-home-content > div > div > div > div:nth-child(1) > div > div > div > div > div > div.card-header > span.survey-title")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str.equals("请输入标题");
    }
    /**
     * 测试一：创建在线考试
     */
    public void createOnlineExams() throws IOException, InterruptedException {
        // 点击“新建”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > span > div > div > button")).click();

        //显式等待：最长等10秒,直到“问卷调查”选项可见
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
        //用 XPath 文本定位
        WebElement surveyItem = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(@class, 'ant-dropdown-menu')]//span[text()='在线考试']")));
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        // 滚动到元素 + JS强制点击
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView({block: 'center'});", surveyItem);
        Thread.sleep(1000);
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", surveyItem);
        Thread.sleep(1000);
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //点击“保存”按钮
        driver.findElement(By.cssSelector("#editor > div.survey-main-panel > div.survey-main-panel-toolbar > div:nth-child(2) > div > button.ant-btn.ant-btn-primary.ant-btn-sm > span")).click();
        //点击"项目"
        driver.findElement(By.cssSelector("#root > div > div.survey-main-navigator > div.actions-container > div:nth-child(6) > a > button > span.anticon.anticon-home > svg")).click();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        //查询是否新建成功
        //在搜索框中输入搜索的内容,点击“查询”按钮
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > input")).sendKeys("考试名称");
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-content > div > div > div > div > div > div > div > span > span > span > button > span > svg")).click();
        //检查是否新建成功
        String str = driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-pro-grid-content > div > div > div > div.ant-list.ant-list-split.ant-list-grid.survey-home-content > div > div > div > div:nth-child(1) > div > div > div > div > div > div.card-header > span.survey-title")).getText();
        //屏幕截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
        assert str.equals("考试名称");
    }
}
